package pl.mmirski.functests.pages;

import static org.junit.Assert.assertEquals;

import java.util.List;

import javax.annotation.Nullable;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.google.common.base.Function;

public abstract class Page {
	
	protected WebDriver driver;

	public Page(WebDriver driver)
	{
		this.driver = driver;
	}
	
	protected void clickOnElement(By by) {
		driver.findElement(by).click();	
	}
	
	public void open(String url) {
		driver.get(url);
	}
	
	
	protected void waitForElement(final By by) {
		WebDriverWait wait = new WebDriverWait(driver, 10);
		
		wait.until(new Function<WebDriver,WebElement>() {

			@Override
			public WebElement apply(@Nullable WebDriver d) {
				return d.findElement(by);
			} 
			 
		});
		
	}
	
	protected void waitForElementOnTheList(final By by, final String text) {
		WebDriverWait wait = new WebDriverWait(driver, 10);
		
		wait.until(new Function<WebDriver,WebElement>() {

			@Override
			public WebElement apply(@Nullable WebDriver d) {
				List<WebElement> elementList = d.findElements(by);
				
				WebElement elementPresent = null;
				
				for (int i = 0; i < elementList.size(); i++) {
					WebElement we = elementList.get(i);
					if (we.getText().equals(text)) {
						elementPresent = we;
					}
				}
				return elementPresent;				
				
			} 
			 
		});
		
	}
	
	protected boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
	
	protected boolean isElementPresentOnTheListByText(By by, String text) {
		List<WebElement> elementList = driver.findElements(by);
		
		for (int i = 0; i < elementList.size(); i++) {
			WebElement we = elementList.get(i);
			if (we.getText().contains(text)) {
				return true;
			}
		}
		return false;
	}
	
	protected void inputText(By by, String text) {
		driver.findElement(by).sendKeys(text);
	}
	
	
	protected void assertTextIsPresent(By by, String text) {
		assertEquals(text,
				driver.findElement(by).getText());		
	}
	
	protected boolean isTextPresent(By by, String text) {
		return driver.findElement(by).getText().contains(text);		
	}

	
	protected void selectValueFromDropDown(By by, String name) {
		Select select = new Select(driver.findElement(by));
		select.selectByVisibleText(name);		
	}
	
	protected String getElementIdByText(By by, String text) {
		
		List<WebElement> elementList = driver.findElements(by);
		
		WebElement elementPresent = null;
		
		for (int i = 0; i < elementList.size(); i++) {
			WebElement we = elementList.get(i);
			if (we.getText().contains(text)) {
				elementPresent = we;
			}
		}
		
		return elementPresent.getAttribute("id");
		
	}
}
